define(["jquery", "okLayer", "okUtils", "okMock"], function($, okLayer, okUtils, okMock) {
    var remoteDomain = $('#api-address').html()
    let table = layui.table;
    let form = layui.form;
    var userTable;
    var userTablebd;

    // 切换商店和本地分页
    var tab = $('.layui-this').attr('data')
    $('#' + tab).show()
    $('.tab').click(function() {
        initPlug();
        $('.pag').hide()
        var tab = $('.layui-this').attr('data')
        if (tab == 'shop') {
            $('#local').show()
        }
        if (tab == 'local') {
            $('#shop').show()
        }
    })

    $('.refresh').click(function(){
        initPlug();
    })

    // 初始化插件
    initPlug();

    // 点击搜索
    $('#search').click(function() {
        initPlug();
    })


    function initPlug() {
        let search = $('#title').val()
        //全部插件
        userTable = table.render({
            elem: '#tableId',
            url: 'getAllplug?search='+search,
            // url: '{:url("getList")}',
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            limit: 10000,
            page: false,
            toolbar: true,
            toolbar: "#toolbarTpl",
            height: 'full',
            cols: [
                [
                    // {field: "intro", title: "标识", width: 100, sort: true},
                    { field: "title", title: "名称" },
                    { field: "version", title: "版本" },
                    { field: "describe", title: "简述" },
                    { field: "author", title: "作者" },
                    { field: "market_price", title: "价格" },
                    { field: "sales", title: "销量" },
                    { field: "status_switch", title: "状态", templet: "#statusTpl" },
                    { field: "button", title: "操作按钮" },
                ]
            ],
            done: function(res, curr, count) {
                // console.log('shop',res, curr, count);
                layui.use('laypage', function() {
                    var laypage_shop = layui.laypage;

                    //执行一个laypage实例
                    laypage_shop.render({
                        elem: 'shop' //注意，这里的 test1 是 ID，不用加 # 号
                            ,
                        count: res.count //数据总数，从服务端得到
                            ,
                        limit: 5,
                        jump: function(obj, first) {
                            //obj包含了当前分页的所有参数，比如：
                            // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                            // console.log(obj.limit); //得到每页显示的条数
                            userTable = table.render({
                                elem: '#tableId',
                                url: 'getAllplug?curr=' + obj.curr+'&search='+search,
                                // url: '{:url("getList")}',
                                cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                                limit: 10000,
                                page: false,
                                toolbar: true,
                                toolbar: "#toolbarTpl",
                                height: 'full',
                                cols: [
                                    [
                                        // {field: "intro", title: "标识", width: 100, sort: true},
                                        { field: "title", title: "名称" },
                                        { field: "version", title: "版本" },
                                        { field: "describe", title: "简述" },
                                        { field: "author", title: "作者" },
                                        { field: "discount_price", title: "价格" },
                                        { field: "sales", title: "销量" },
                                        { field: "status_switch", title: "状态", templet: "#statusTpl" },

                                        { field: "button", title: "操作按钮" },
                                    ]
                                ],
                                done: function(res, curr, count) {
                                    // console.info(res, curr, count);
                                }
                            });

                            //首次不执行
                            if (!first) {
                                //do something
                            }
                        }
                    });
                });
            }
        });

        //本地插件
        userTablebd = table.render({
            elem: '#tableIdbd',
            url: 'getList?search='+search,
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            limit: 10000,
            page: false,
            toolbar: true,
            toolbar: "#toolbarTpl",
            height: 'full',
            cols: [
                [
                    //{field: "name", title: "标识", width: 100, sort: true},
                    { field: "title", title: "名称" },
                    { field: "version", title: "版本" },
                    { field: "description", title: "简述" },
                    { field: "author", title: "作者" },

                    { field: "status", title: "状态", templet: "#statusTpl" },

                    { field: "button", title: "操作按钮" },
                ]
            ],
            done: function(res, curr, count) {
                // console.log('local',res, curr, count);
                layui.use('laypage', function() {
                    var laypage_local = layui.laypage;

                    //执行一个laypage实例
                    laypage_local.render({
                        elem: 'local' //注意，这里的 test1 是 ID，不用加 # 号
                            ,
                        count: res.count //数据总数，从服务端得到
                            ,
                        limit: 5,
                        jump: function(obj, first) {
                            //obj包含了当前分页的所有参数，比如：
                            // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                            // console.log(obj.limit); //得到每页显示的条数
                            userTablebd = table.render({
                                elem: '#tableIdbd',
                                url: 'getList?curr=' + obj.curr+'&search='+search,
                                cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                                limit: 10000,
                                page: false,
                                toolbar: true,
                                toolbar: "#toolbarTpl",
                                height: 'full',
                                cols: [
                                    [
                                        //{field: "name", title: "标识", width: 100, sort: true},
                                        { field: "title", title: "名称" },
                                        { field: "version", title: "版本" },
                                        { field: "description", title: "简述" },
                                        { field: "author", title: "作者" },

                                        { field: "status", title: "状态", templet: "#statusTpl" },

                                        { field: "button", title: "操作按钮" },
                                    ]
                                ],
                                done: function(res, curr, count) {
                                    // console.info(res, curr, count);

                                }
                            });

                            //首次不执行
                            if (!first) {
                                //do something
                            }
                        }
                    });
                });
            }
        });
    }


    // form.on("submit(search)", function(data) {
    //     userTable.reload({
    //         where: data.field,
    //         page: { curr: 1 }
    //     });
    //     return false;
    // });

    table.on("toolbar(tableFilter)", function(obj) {
        switch (obj.event) {
            case "batchEnabled":
                batchEnabled();
                break;
            case "batchDisabled":
                batchDisabled();
                break;
            case "batchDel":
                batchDel();
                break;
            case "add":
                add();
                break;
        }
    });

    table.on("tool(tableFilter)", function(obj) {
        let data = obj.data;
        switch (obj.event) {
            case "install":
                install(data.name);
                break;
            case "uninstall":
                uninstall(data.name);
                break;
            case "config":
                config(data.name);
                break;
            case "state":
                state(data.name);
                break;
            case "remoteInstall":
                remoteInstall(data.name);
                break;
        }
    });

    //安装远程插件
    function remoteInstall(id) {
        const Storage = {};
        Storage.get = function(name) {
            return localStorage.getItem(name);
        }
        var mango_tokeny = Storage.get("mangoadmin_userinfo");
        if (!mango_tokeny) {
            return okLayer.confirm("你当前未登录MangoAdmin，登录后将同步已购买的记录", function() {
                layer.close(layer.index);
                layer.open({
                    type: 2,
                    title: '用户登录',
                    maxmin: true,
                    skin: 'layui-layer-lan',
                    shadeClose: true, //点击遮罩关闭层
                    area: ['600px', '480px'],
                    content: '/admin/mango/login.html' //弹框显示的url
                });
            })
        }
        // 查询多个版本
        // layer.open({
        //     title: '历史版本'
        //     , btn: ['确定']
        //     ,area:['300px', '200px']
        //     , content:
        //         '<form class="layui-form">'+
        //         '<select name="range" id="range" lay-filter="range">' +
        //         '<option value="0">1.0.1</option>' +
        //         '<option value="1">1.0.12</option>' +
        //         '<option value="2">1.0.13</option>' +
        //         '<option value="2">1.0.13</option>' +
        //         '<option value="2">1.0.13</option>' +
        //         '<option value="2">1.0.13</option>' +
        //         '<option value="2">1.0.13</option>' +
        //         '</select>'+'</form>',
        //         success: function(layero, index){
        //             // 重新渲染弹层中的下拉选择框select
        //             form.render('select');
        //         },
        //         yes: function (index,layero) {
        //             alert($('#range').val());
        //         }});
        // return false
        okLayer.confirm("确定要安装吗？", function() {
            okUtils.ajax("remoteInstall", "get", { id: id, token: JSON.parse(mango_tokeny).token }, true).done(function(response) {
                if (response.code == '1') {
                    //userTable.reload();
                    //okUtils.tableSuccessMsg(response.msg);
                    okLayer.greenTickMsg(response.msg, function() {
                        userTable.reload();
                        userTablebd.reload();
                    })
                } else {

                    if (response.code == '401') {
                        //未登录
                        layer.open({
                            type: 2,
                            title: '用户登录',
                            maxmin: true,
                            skin: 'layui-layer-lan',
                            shadeClose: true, //点击遮罩关闭层
                            area: ['600px', '480px'],
                            content: '/admin/mango/login.html' //弹框显示的url
                        });
                    } else {
                        okLayer.redCrossMsg(response.msg, function() {})
                    }


                    if (response.code == '-5') {
                        //未购买
                        okLayer.redCrossMsg(response.msg, function() {
                            var html = "<img style='height: 200px;width: 200px;' src='" + response.data.img + "' >";
                            layer.open({
                                type: 2,
                                title: '请使用手机扫码二维码支付',
                                shade: [0.001, '#393D49'],
                                area: ['500px', '500px'],
                                // skin : 'layui-layer-nobg', //沒有背景色
                                shadeClose: true,
                                content: '/admin/mango/payqr.html?img=' + response.data.code_url + '&order_sn=' + response.data.order_sn + '&name=' + response.data.name //弹框显示的url
                            });
                        });
                    } else {
                        okLayer.redCrossMsg(response.msg, function() {})
                    }

                }

            }).fail(function(error) {
                // console.log(error)
            });
        })
    }

    //安装本地插件
    function install(id) {
        okLayer.confirm("确定要安装吗？", function() {
            okUtils.ajax("install", "get", { id: id }, true).done(function(response) {
                if (response.code == 1) {
                    // console.log(response);
                    //userTable.reload();
                    //okUtils.tableSuccessMsg(response.msg);
                    okLayer.greenTickMsg(response.msg, function() {
                        userTable.reload();
                        userTablebd.reload();
                    })
                } else {
                    okLayer.redCrossMsg(response.msg, function() {

                    })
                }

            }).fail(function(error) {
                // console.log(error)
            });
        })
    }

    function config(id) {
        okLayer.open("配置", "config?name=" + id, "90%", "90%", function(layero) {
            //let iframeWin = window[layero.find("iframe")[0]["name"]];
            //iframeWin.initForm(data);
        }, function() {
            userTable.reload();
            userTablebd.reload();
        })
    }

    function uninstall(id) {
        okLayer.confirm("确定要卸载安装吗？", function() {
            okUtils.ajax("uninstall", "get", { id: id }, true).done(function(response) {
                if (response.code == 1) {
                    // console.log(response);
                    //userTable.reload();
                    //okUtils.tableSuccessMsg(response.msg);
                    okLayer.greenTickMsg(response.msg, function() {
                        userTable.reload();
                        userTablebd.reload();
                    })
                } else {
                    okLayer.greenTickMsg(response.msg, function() {

                    })
                }

            }).fail(function(error) {
                // console.log(error)
            });
        })
    }

    function state(id) {
        okLayer.confirm("确定执行此操作吗？", function() {
            okUtils.ajax("state", "get", { id: id }, true).done(function(response) {
                if (response.code == 1) {
                    // console.log(response);
                    //userTable.reload();
                    //okUtils.tableSuccessMsg(response.msg);
                    okLayer.greenTickMsg(response.msg, function() {
                        userTable.reload();
                        userTablebd.reload();
                    })
                } else {
                    okLayer.greenTickMsg(response.msg, function() {

                    })
                }

            }).fail(function(error) {
                // console.log(error)
            });
        })
    }

    // 同步信息
    $('.get-user').click(function() {
        const Storage = {};
        Storage.get = function(name) {
            return localStorage.getItem(name);
        }
        Storage.set = function(name, val) {
            localStorage.setItem(name, val);
        }
        var mango_tokeny = Storage.get("mangoadmin_userinfo");
        if (!mango_tokeny) {
            layer.open({
                type: 2,
                title: '用户登录',
                maxmin: true,
                skin: 'layui-layer-lan',
                shadeClose: true, //点击遮罩关闭层
                area: ['600px', '480px'],
                content: '/admin/mango/login.html' //弹框显示的url
            });
        } else {
            // 请求接口
            okUtils.ajax(remoteDomain + "/api/user/index.html", "post", { token: JSON.parse(mango_tokeny).token }, true).done(function(response) {
                // console.log(response,111);
                if (response.code == 1) {
                    Storage.set("mangoadmin_userinfo", JSON.stringify(response.data.userinfo));
                    var mango_tokeny = Storage.get("mangoadmin_userinfo");
                    $('.score').text(JSON.parse(mango_tokeny).score)
                    layer.open({
                        content: JSON.parse(mango_tokeny).username,
                        btn: ['退出', '取消'],
                        yes: function(index, layero) {
                            //按钮【按钮一】的回调
                            localStorage.removeItem("mangoadmin_userinfo");
                            layer.msg('退出成功', { time: 1500 })
                            setTimeout(layer.close(index), 800)
                        },
                        btn2: function(index, layero) {
                            //按钮【按钮二】的回调

                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        cancel: function() {
                            //右上角关闭回调

                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    });
                } else {
                    okLayer.redCrossMsg(response.msg, function() {

                    })
                }

            }).fail(function(error) {
                localStorage.removeItem("mangoadmin_userinfo");
            });

        }
    })

    form.verify({

    });

    form.on("submit(add)", function(data) {
        okUtils.ajax($('#plugs_action').attr('action'), "post", data.field, true).done(function(response) {
            //console.log(response);
            if (response.code == 1) {
                okLayer.greenTickMsg("编辑成功", function() {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            } else {
                okLayer.greenTickMsg(response.msg, function() {

                })
            }

        }).fail(function(error) {
            // console.log(error)
        });
        return false;
    });

    var Controller = {
        index: function() {}
    }
    return Controller;
})